package com.mine.site.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mine.site.dao.CommodityDao;
import com.mine.site.dao.ShoppingCartDao;
import com.mine.site.dao.UserDao;
import com.mine.site.model.Commodity;
import com.mine.site.model.User;




@WebServlet(urlPatterns="/UserServlet")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("utf-8");
		//jsp��session�����ö���
		HttpSession session = request.getSession();
		User u = (User)session.getAttribute("loginUser");
		if(u == null){
			ShoppingCartDao dao = new ShoppingCartDao();
			dao.delete1();
		
		}
		ServletContext ctx = request.getServletContext();
		//jsp  out�����ö���
		PrintWriter out =  response.getWriter();
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String method = request.getParameter("m");
		if("del".equals(method)){
			this.del(request, response);
		}else if("forEdit".equals(method)){
			this.beforeEdit(request, response);
		}else if("update".equals(method)){
			this.update(request, response);
		}else if("save".equals(method)){
			this.save(request, response);
		}else if("byname".equals(method)){
			this.bynamme(request, response);
		}
		else{
			this.list(request, response);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
	
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

		CommodityDao dao = new CommodityDao();
		List<Commodity> Commo = dao.findAll();
		request.setAttribute("CommoList", Commo);
		
		List<Commodity> Commo_clnz = dao.findAll_clvz();
		request.setAttribute("CommoList_clnz", Commo_clnz);
		
		List<Commodity> Commo_lyq = dao.findAll_lyq();
		request.setAttribute("CommoList_lyq", Commo_lyq);
	
		List<Commodity> Commo_clnanz = dao.findAll_clnanz();
		request.setAttribute("CommoList_clnanz", Commo_clnanz);
		
		List<Commodity> Commo_nantx = dao.findAll_nantx();
		request.setAttribute("CommoList_nantx", Commo_nantx);
		
		List<Commodity> Commo_nvtx = dao.findAll_nvtx();
		request.setAttribute("CommoList_nvtx", Commo_nvtx);
		
		List<Commodity> Commo_jpnk = dao.findAll_jpnk();
		request.setAttribute("CommoList_jpnk", Commo_jpnk);
		
		List<Commodity> Commo_jonk = dao.findAll_jonk();
		request.setAttribute("CommoList_jonk", Commo_jonk);
		
		List<Commodity> Commo_cjwt = dao.findAll_cjwt();
		request.setAttribute("CommoList_cjwt", Commo_cjwt);
		
		List<Commodity> Commo_xlbx = dao.findAll_xlbx();
		request.setAttribute("CommoList_xlbx", Commo_xlbx);
		

		request.getRequestDispatcher("index.jsp").forward(request, response);
	}
	
	private void bynamme(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String name = request.getParameter("name");
		
		UserDao dao = new UserDao();
		List<User> users = dao.findByName(name);
		request.setAttribute("userList", users);
		request.getRequestDispatcher("listUser.jsp").forward(request, response);
	}
	
	private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String loginId = request.getParameter("loginId");
		UserDao dao = new UserDao();
		dao.delete(loginId);
		response.sendRedirect("UserServlet");
	}
	
	private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		User u = wrapUser(request);
		String id=UUID.randomUUID().toString().replaceAll("-", "");
		u.setLoginId(id);
		UserDao dao = new UserDao();
		dao.save(u);

		response.sendRedirect("rs.jsp");
	}
	
	private void beforeEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String Id = request.getParameter("Id");
		UserDao dao = new UserDao();
		User u = dao.findById(Id);
		request.setAttribute("user", u);
		request.getRequestDispatcher("editbook.jsp").forward(request, response);

	}
	
	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		User u = wrapUser(request);
		UserDao dao = new UserDao();
		dao.update(u);
		response.sendRedirect("UserServlet");
	}

	private User wrapUser(HttpServletRequest request) {
		User u = new User();
		u.setLoginId(request.getParameter("loginId"));
		u.setName(request.getParameter("Name"));
		u.setLoginPwd(request.getParameter("loginPwd"));
		u.setPhone(request.getParameter("phone"));
		u.setEmail(request.getParameter("email"));
		u.setAddress(request.getParameter("address"));
		return u;
	}
}
